﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DbView
{
	/*
		Todo:
		1. add into the menu
		2. document.
		3. copy the template file as well 
			- perhaps refactor to add to resources.
			
	*/
	public partial class MakePortableAppForm : Form
	{
		public MakePortableAppForm()
		{
			InitializeComponent();
			lblHelp.Text = TITLE;
			checkConnections.Checked = true;
		}

		private void btnBrowse_Click(object sender, EventArgs e)
		{
			// open a select folder dialo
			FolderBrowserDialog dlg = new FolderBrowserDialog();
			// Set the help text description for the FolderBrowserDialog. 
			dlg.Description = "Select the folder that will contain the portable version of DbViewSharp";

			// Do not allow the user to create new files via the FolderBrowserDialog. 
			dlg.ShowNewFolderButton = false;

			// Default to the My Documents folder. 
			dlg.RootFolder = Environment.SpecialFolder.Desktop;

			DialogResult result = dlg.ShowDialog(this);
			if (result == DialogResult.OK)
			{
				txtFolderName.Text = dlg.SelectedPath;
			}
		}

		private void btnClose_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void btnGo_Click(object sender, EventArgs e)
		{
			// get data from controls
			String folderName = txtFolderName.Text;
			if (folderName.Length == 0)
			{
				// message please
				MessageBox.Show("You need to select the folder that will contain the portable version of DbViewSharp", "Cannot proceed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				return;
			}
			bool copyConnections = checkConnections.Checked;
			bool copyDocs = checkDocs.Checked;
			Generate(folderName, copyConnections, copyDocs);
		}

		private void Generate(string folderName, bool copyConnections, bool copyDocs)
		{
			String message;
			//			String folderSubDirectory = DataFolderName();
			String folderSubDirectory = ApplicationCopier.DataFolderName();

			// Invoke the engine.
			bool success = ApplicationCopier.Copy(folderName, folderSubDirectory, copyConnections, copyDocs, out message);
			if (success)
			{
				String program = ApplicationCopier.PortableExeName(folderName, folderSubDirectory);
				message = "Portable copy created sucessfully. The program to launch is:\n" + program;
				MessageBox.Show(message, "Finished", MessageBoxButtons.OK, MessageBoxIcon.Information);
			}
			else
			{
				MessageBox.Show(message, "Failed to create portable DbViewSharp", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		const string TITLE = "Use this dialog to create a portable version of DbViewSharp on a USB stick. " +
							 "Enter the name of an existing folder or browse to it. " +
							 "Check the top box to copy the current database connections to the new location. " +
							 "Check the lower box if you need to acceess the documenation. " +
							 "When you press [ Generate ] the program will attempt to create a new DBVIEW subfolder and copy the required program files into it. " +
							 "Once this is complete you should be able to browse to that folder and execute DbView3.exe to launch DbViewSharp. ";
	}
}
